package com.sailfish.springbootdemo.dao.db1;

import com.sailfish.springbootdemo.common.DB1TableName;
import com.sailfish.springbootdemo.pojo.db1.AuditHistory;
import com.sailfish.springbootdemo.pojo.db1.AuditRecord;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface AuditRecordDao {

    /**
     * 查询
     *
     * @return
     */
    @Results(id = "auditRecordResultMap", value = {
            @Result(column = "audit_record_id", property = "auditRecordId", jdbcType = JdbcType.INTEGER),
            @Result(column = "related_type", property = "relatedType", jdbcType = JdbcType.INTEGER),
            @Result(column = "related_id", property = "relatedId", jdbcType = JdbcType.INTEGER),
            @Result(column = "submit_user_id", property = "submitUserId", jdbcType = JdbcType.INTEGER),
            @Result(column = "audit_user_id", property = "auditUserId", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_user_id", property = "createUserId", jdbcType = JdbcType.INTEGER),
            @Result(column = "audit_status", property = "auditStatus", jdbcType = JdbcType.INTEGER),
            @Result(column = "change_json", property = "changeJson", jdbcType = JdbcType.VARCHAR),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "submitter_time", property = "submitterTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "audit_time", property = "auditTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
    })
    @Select("<script>" +
            "select * from " + DB1TableName.AUDIT_RECORD + " where 1 = 1 " +
            "<if test='relatedType != null and relatedType != \"\" '>" +
            " and related_type = #{relatedType} " +
            "</if>" +
            "<if test='relatedId != null and relatedId != \"\" '>" +
            " and related_id = #{relatedId} " +
            "</if>" +
            " order by create_time desc,submitter_time desc,audit_time desc" +
            "<if test='begin != null and pageSize != null and pageSize != \"\" '>" +
            " limit #{begin}, #{pageSize}" +
            "</if>" +
            "</script>")
    List<AuditRecord> getAuditRecord(@Param("relatedType") Integer relatedType, @Param("relatedId") Integer relatedId, @Param("begin") Integer begin, @Param("pageSize") Integer pageSize);

    /**
     * 新增
     *
     * @return
     */
    @Insert("Insert into " + DB1TableName.AUDIT_RECORD + " (related_type,related_id,submit_user_id,audit_user_id,audit_status,change_json,remark,submitter_time,audit_time,create_time,create_user_id) values " +
            " (#{auditRecord.relatedType},#{auditRecord.relatedId},#{auditRecord.submitUserId},#{auditRecord.auditUserId},#{auditRecord.auditStatus},#{auditRecord.changeJson},#{auditRecord.remark},#{auditRecord.submitterTime},#{auditRecord.auditTime},#{auditRecord.createTime},#{auditRecord.createUserId})")
    Integer addAuditRecord(@Param("auditRecord") AuditRecord auditRecord);
}
